package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionEx extends JFrame {

	private JPanel contentPane;
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField addIn;
	private JScrollPane scrollPane;
	private JTextArea result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UrlConnectionEx frame = new UrlConnectionEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	public void process(){
		String d = addIn.getText();
		String ad;
		try{
			if(!d.contains("http://")){
				ad = "http://" + d;  
			}else{
				ad=d;
			}
			URL url = new URL(ad);
			result.append("호스트명 : " + url.getHost() + "\n");
			result.append("경로 : " + url.getPath() + "\n");
			result.append("포트 : " + url.getPort() + "\n");
			result.append("프로토콜 : " + url.getProtocol() + "\n");
			result.append("쿼리 : " + url.getQuery() + "\n");
			result.append("유저정보 : " + url.getUserInfo() + "\n");
			result.append("권한 : " + url.getAuthority() + "\n\n================================================\n");
			
			URLConnection uc = url.openConnection();
			result.append("Mime Type : " + uc.getContentType() + "\n");
			result.append("Encoding : " + uc.getContentEncoding() + "\n");
			result.append("Header : " + uc.getHeaderFields() + "\n");
			
			InputStream is = uc.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			BufferedReader br = new BufferedReader(isr);
			
			String str = "";
			while(true){
				str = br.readLine();
				if(str == null) break;
				
				result.append(str + "\n");
			}
			br.close();
			isr.close();
			is.close();
			
		}catch(MalformedURLException me){
			JOptionPane.showMessageDialog(null, me.getMessage());
		}catch(IOException ie){
			JOptionPane.showMessageDialog(null, ie.getMessage());
		}
	}
	
	public UrlConnectionEx() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 708, 514);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getAddIn(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Get");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					process();
				}
			});
		}
		return btnNewButton;
	}
	private JTextField getAddIn() {
		if (addIn == null) {
			addIn = new JTextField();
			addIn.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					int a = e.getKeyCode();
					if(a == e.VK_ENTER){
						process();
						
					}
				}
			});
			addIn.setColumns(10);
		}
		return addIn;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
}
